<?php

ini_set("display_errors", 1);
ini_set("track_errors", 1);
ini_set("html_errors", 1);
error_reporting(E_ALL);

/*require_once ('includes/clanconfig.php');*/

$host = 'localhost';
$db = 'sthlm';
$userDB = 'root';
$passDB = 'r7vk0li';
$wotServer = 'worldoftanks.ru';
$clanID = '152414';
$appid = '34e343d1168159c4c27140b5f13b66d5';

/*
Connect to DB
 */

$db_connect = mysqli_connect($host, $userDB, $passDB, $db);

$url = "https://api.{$wotServer}/wot/clan/info/?application_id={$appid}&clan_id={$clanID}";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = json_decode(curl_exec($ch), true);

/*
Clan info
 */

foreach ($data['data'] as $clanID => $clanIDData) {
    $url = "https://api.{$wotServer}/wot/clan/info/?application_id={$appid}&clan_id={$clanID}";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $clanData = json_decode(curl_exec($ch), true);
    echo "This is clanData ARRAY:";
    echo "<pre>", print_r($clanData), "</pre>";
    $description_html = addslashes($clanIDData['description_html']);
    mysqli_query($db_connect, "INSERT INTO clan_info (`clanID`, `name`, `abbr`, `description`, `members_count`, `color`, `created_at`, `motto`, `emblem`) VALUES ('" . $clanID . "','" . $clanIDData['name'] . "','" . $clanIDData['abbreviation'] . "', '" . $description_html . "' , '" . $clanIDData['members_count'] . "', '" . $clanIDData['color'] . "', '" . $clanIDData['created_at'] . "', '" . $clanIDData['motto'] . "','" . $clanIDData['emblems']['large'] . "')");

    foreach ($data['data'][$clanID]['members'] as $memberID => $memberInfo) {
        $stats_url = "https://api.{$wotServer}/wot/ratings/accounts/?application_id={$appid}&type=all&account_id={$memberID}";
        mysqli_query($db_connect, "INSERT INTO player_info (`playerID`, `clanID`, `name`, `role`, `role_i18n`, `stats_url`, `epoch`) VALUES ('" . $memberInfo['account_id'] . "', '" . $clanID . "', '" . $memberInfo['account_name'] . "', '" . $memberInfo['role'] . "', '" . $memberInfo['role_i18n'] . "', '" . $stats_url . "', '" . $memberInfo['created_at'] . "')");
    }
}

$request = mysqli_query($db_connect, "SELECT * FROM `player_info`");
    
    while ($row = mysqli_fetch_array($request)) {
        $url = $row['stats_url'];
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $statArray = json_decode(curl_exec($ch), true);

        /*echo "This is clanData ARRAY:";
        echo "<pre>", print_r($statArray), "</pre>";*/

        foreach ($statArray['data'] as $accStatsID => $accStatsValue) {
            /*$exist = mysqli_query($db_connect, "SELECT * FROM player_info");*/
            if (!mysqli_query($db_connect, "UPDATE player_info SET all_winrate=('". $accStatsValue['wins_ratio']['value'] ."') WHERE playerID = $accStatsID ")) {
                echo ("Error:". mysqli_error($db_connect));
            }
            if (!mysqli_query($db_connect, "UPDATE player_info SET all_battles=('".$accStatsValue['battles_count']['value'] ."'), all_battle_avg_xp=('".$accStatsValue['xp_avg']['value'] ."') WHERE playerID = $accStatsID ")) {
                echo ("Error:". mysqli_error($db_connect));
            }

            echo "<pre>", print_r($accStatsValue), "</pre>";
        }
}

